//
//  HomeTableViewController.swift
//  WeiBo(Swift)
//
//  Created by 周成龙 on 16/9/18.
//  Copyright © 2016年 ZCL. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    // MARK: - 懒加载
    private lazy var popoViewManger: PopoverAnimationMenager = {
        let anim = PopoverAnimationMenager()
        anim.presentFrame = CGRect(x: UIScreen.main.bounds.width*0.5 - 100, y: 45, width: 200, height: 200)
        return anim
    }()
    
    private lazy var titleButton: TitleButton = {
        let btn = TitleButton()
        let title = UserAccount.loadAccount()?.screen_name
        btn.setTitle(title, for: UIControlState.normal)
        btn.addTarget(self, action: #selector(HomeTableViewController.titleButtonClick(btn:)), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    // MARK: - 控制器生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            visitorView?.setupVisitorView(imageName: nil, title: "关注一些人，回这里看有什么惊喜")
            return
        }
        
        setupUI()
        
        NotificationCenter.default.addObserver(self, selector: #selector(HomeTableViewController.didreceiveNotification), name: NSNotification.Name(rawValue: kPopoverDidPresentNotification), object: popoViewManger)
        NotificationCenter.default.addObserver(self, selector: #selector(HomeTableViewController.didreceiveNotification), name: NSNotification.Name(rawValue: kPopoverDidDismissNotification), object: popoViewManger)
    }
    
    // MARK: - UI
    private func setupUI() {
        let leftItem = UIBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(HomeTableViewController.leftButtonClick))
        navigationItem.leftBarButtonItem = leftItem
        
        let rightItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightButtonClick))
        navigationItem.rightBarButtonItem = rightItem
        
        navigationItem.titleView = titleButton
    }
    
    // MARK: - 移除通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: - 通知回调
    @objc private func didreceiveNotification() {
        titleButton.isSelected = !titleButton.isSelected
        ZLog(message: "")
    }
    
    // MARK: - 内部方法 导航栏方法
    @objc private func leftButtonClick() {
        ZLog(message: "")
    }
    
    @objc private func rightButtonClick() {
        ZLog(message: "")
    }
    
    @objc private func titleButtonClick(btn: UIButton) {
        
        // 1.修改btn状态
//        btn.isSelected = !btn.isSelected
        // 2. 加载vc
        let sb = UIStoryboard(name: "Popover", bundle: nil)
        guard let menuView = sb.instantiateInitialViewController() else {
            return
        }
        
        // 3. 自定义专场动画
        menuView.transitioningDelegate = popoViewManger
        menuView.modalPresentationStyle = UIModalPresentationStyle.custom
        
        present(menuView, animated: true, completion: nil)
    }
    
    // MARK: - Table view data source
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    /*
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
     
     // Configure the cell...
     
     return cell
     }
     */
}





